# Copyright 2021 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

load(
    "//build_tools/bazel:build_defs.oss.bzl",
    "iree_compiler_cc_library",
    "iree_gentbl_cc_library",
    "iree_tablegen_doc",
)

package(
    default_visibility = ["//visibility:public"],
    features = ["layering_check"],
    licenses = ["notice"],  # Apache 2.0
)

################################################################################
# LLVMGPU common
################################################################################

iree_gentbl_cc_library(
    name = "PassesIncGen",
    tbl_outs = [
        (
            ["--gen-pass-decls"],
            "Passes.h.inc",
        ),
    ],
    tblgen = "@llvm-project//mlir:mlir-tblgen",
    td_file = "Passes.td",
    deps = ["@llvm-project//mlir:PassBaseTdFiles"],
)

iree_compiler_cc_library(
    name = "PassHeaders",
    hdrs = [
        "Passes.h",
        "Passes.h.inc",
    ],
    deps = [
        ":PassesIncGen",
        "//compiler/src/iree/compiler/Codegen/Dialect/Codegen/IR:IREECodegenDialect",
        "//compiler/src/iree/compiler/Dialect/HAL/IR",
        "//compiler/src/iree/compiler/Utils",
        "@llvm-project//mlir:LinalgTransforms",
        "@llvm-project//mlir:MemRefDialect",
        "@llvm-project//mlir:NVGPUDialect",
        "@llvm-project//mlir:Pass",
        "@llvm-project//mlir:Transforms",
    ],
)

################################################################################
# ROCDL specific
################################################################################

iree_gentbl_cc_library(
    name = "ROCDLPassesIncGen",
    tbl_outs = [
        (
            ["--gen-pass-decls"],
            "ROCDLPasses.h.inc",
        ),
    ],
    tblgen = "@llvm-project//mlir:mlir-tblgen",
    td_file = "ROCDLPasses.td",
    deps = ["@llvm-project//mlir:PassBaseTdFiles"],
)

iree_compiler_cc_library(
    name = "ROCDLPassHeaders",
    hdrs = [
        "ROCDLPasses.h",
        "ROCDLPasses.h.inc",
    ],
    deps = [
        ":ROCDLPassesIncGen",
        "//compiler/src/iree/compiler/Dialect/HAL/IR",
        "@llvm-project//mlir:Pass",
    ],
)

################################################################################
# Common library
################################################################################

iree_compiler_cc_library(
    name = "LLVMGPU",
    srcs = [
        "AMDGPUEmulateNarrowType.cpp",
        "ConvertToLLVM.cpp",
        "ConvertToNVVM.cpp",
        "ConvertToROCDL.cpp",
        "ExtractAddressComputationGPUPass.cpp",
        "KernelConfig.cpp",
        "LLVMGPUAssignConstantOrdinals.cpp",
        "LLVMGPUCastAddressSpaceFunction.cpp",
        "LLVMGPUCastTypeToFitMMA.cpp",
        "LLVMGPUConfigureTensorLayouts.cpp",
        "LLVMGPULinkExecutables.cpp",
        "LLVMGPULowerExecutableTarget.cpp",
        "LLVMGPUPackSharedMemoryAlloc.cpp",
        "LLVMGPUSelectLoweringStrategy.cpp",
        "LLVMGPUTensorCoreVectorization.cpp",
        "LLVMGPUTileAndDistribute.cpp",
        "LLVMGPUVectorDistribute.cpp",
        "LLVMGPUVectorLowering.cpp",
        "LLVMGPUVectorToGPU.cpp",
        "Passes.cpp",
        "ROCDLAnnotateKernelForTranslation.cpp",
        "ROCDLBufferInstructionsOptimization.cpp",
        "ROCDLConfigureBufferInstructions.cpp",
        "ROCDLLowerExecutableTarget.cpp",
        "ROCDLPrefetching.cpp",
        "Verifiers.cpp",
    ],
    hdrs = [
        "ConvertToLLVM.h",
        "KernelConfig.h",
        "Passes.h",
        "ROCDLPasses.h",
    ],
    deps = [
        ":PassHeaders",
        ":PassesIncGen",
        ":ROCDLPassHeaders",
        ":ROCDLPassesIncGen",
        "//compiler/src/iree/compiler/Codegen/Common",
        "//compiler/src/iree/compiler/Codegen/Common:TransformDialectInterpreterPass",
        "//compiler/src/iree/compiler/Codegen/Common/GPU:CommonGPUPasses",
        "//compiler/src/iree/compiler/Codegen/Common/GPU:GPUHeuristics",
        "//compiler/src/iree/compiler/Codegen/Dialect/Codegen/IR:IREECodegenDialect",
        "//compiler/src/iree/compiler/Codegen/Dialect/GPU/IR:IREEGPUDialect",
        "//compiler/src/iree/compiler/Codegen/Dialect/GPU/TargetUtils:ConfigUtils",
        "//compiler/src/iree/compiler/Codegen/Dialect/GPU/Transforms:GPUTransforms",
        "//compiler/src/iree/compiler/Codegen/Dialect/VectorExt/IR:IREEVectorExtDialect",
        "//compiler/src/iree/compiler/Codegen/Dialect/VectorExt/Transforms:VectorExtTransforms",
        "//compiler/src/iree/compiler/Codegen/Interfaces:PartitionableLoopsInterface",
        "//compiler/src/iree/compiler/Codegen/Interfaces:UKernelOpInterface",
        "//compiler/src/iree/compiler/Codegen/LLVMGPU/TransformExtensions:LLVMGPUExtensions",
        "//compiler/src/iree/compiler/Codegen/LLVMGPU/Utils",
        "//compiler/src/iree/compiler/Codegen/Transforms",
        "//compiler/src/iree/compiler/Codegen/Utils",
        "//compiler/src/iree/compiler/Codegen/Utils:VectorOpUtils",
        "//compiler/src/iree/compiler/Dialect/HAL/IR",
        "//compiler/src/iree/compiler/Dialect/HAL/Transforms",
        "//compiler/src/iree/compiler/Dialect/LinalgExt/IR",
        "//compiler/src/iree/compiler/Dialect/LinalgExt/Transforms",
        "//compiler/src/iree/compiler/Dialect/LinalgExt/Utils",
        "//compiler/src/iree/compiler/Dialect/TensorExt/IR",
        "//compiler/src/iree/compiler/Dialect/Util/IR",
        "//compiler/src/iree/compiler/Dialect/Util/Transforms",
        "//compiler/src/iree/compiler/Utils",
        "//llvm-external-projects/iree-dialects:IREELinalgTransformDialect",
        "//llvm-external-projects/iree-dialects:IREELinalgTransformDialectPasses",
        "@llvm-project//llvm:Support",
        "@llvm-project//mlir:AMDGPUDialect",
        "@llvm-project//mlir:AMDGPUToROCDL",
        "@llvm-project//mlir:AMDGPUTransforms",
        "@llvm-project//mlir:AMDGPUUtils",
        "@llvm-project//mlir:AffineDialect",
        "@llvm-project//mlir:AffineToStandard",
        "@llvm-project//mlir:AffineTransforms",
        "@llvm-project//mlir:Analysis",
        "@llvm-project//mlir:ArithDialect",
        "@llvm-project//mlir:ArithToAMDGPU",
        "@llvm-project//mlir:ArithToLLVM",
        "@llvm-project//mlir:ArithTransforms",
        "@llvm-project//mlir:BufferizationDialect",
        "@llvm-project//mlir:BufferizationTransforms",
        "@llvm-project//mlir:ComplexToLLVM",
        "@llvm-project//mlir:ComplexToStandard",
        "@llvm-project//mlir:ControlFlowToLLVM",
        "@llvm-project//mlir:DialectUtils",
        "@llvm-project//mlir:FuncDialect",
        "@llvm-project//mlir:FuncToLLVM",
        "@llvm-project//mlir:FuncTransforms",
        "@llvm-project//mlir:FunctionInterfaces",
        "@llvm-project//mlir:GPUDialect",
        "@llvm-project//mlir:GPUToNVVMTransforms",
        "@llvm-project//mlir:GPUToROCDLTransforms",
        "@llvm-project//mlir:GPUTransformOps",
        "@llvm-project//mlir:GPUTransforms",
        "@llvm-project//mlir:IR",
        "@llvm-project//mlir:LLVMCommonConversion",
        "@llvm-project//mlir:LLVMDialect",
        "@llvm-project//mlir:LinalgDialect",
        "@llvm-project//mlir:LinalgTransforms",
        "@llvm-project//mlir:MathDialect",
        "@llvm-project//mlir:MathToLLVM",
        "@llvm-project//mlir:MathToROCDL",
        "@llvm-project//mlir:MathTransforms",
        "@llvm-project//mlir:MemRefDialect",
        "@llvm-project//mlir:MemRefToLLVM",
        "@llvm-project//mlir:MemRefTransforms",
        "@llvm-project//mlir:NVGPUDialect",
        "@llvm-project//mlir:NVGPUToNVVM",
        "@llvm-project//mlir:NVGPUTransforms",
        "@llvm-project//mlir:NVGPUUtils",
        "@llvm-project//mlir:NVVMDialect",
        "@llvm-project//mlir:NVVMToLLVM",
        "@llvm-project//mlir:PDLDialect",
        "@llvm-project//mlir:PDLInterpDialect",
        "@llvm-project//mlir:Pass",
        "@llvm-project//mlir:ROCDLDialect",
        "@llvm-project//mlir:ROCDLTarget",
        "@llvm-project//mlir:ROCDLToLLVMIRTranslation",
        "@llvm-project//mlir:SCFDialect",
        "@llvm-project//mlir:SCFToControlFlow",
        "@llvm-project//mlir:SCFTransforms",
        "@llvm-project//mlir:SideEffectInterfaces",
        "@llvm-project//mlir:Support",
        "@llvm-project//mlir:TensorDialect",
        "@llvm-project//mlir:TensorTransforms",
        "@llvm-project//mlir:TransformDialect",
        "@llvm-project//mlir:TransformUtils",
        "@llvm-project//mlir:Transforms",
        "@llvm-project//mlir:UBDialect",
        "@llvm-project//mlir:UBToLLVM",
        "@llvm-project//mlir:ValueBoundsOpInterface",
        "@llvm-project//mlir:VectorDialect",
        "@llvm-project//mlir:VectorToGPU",
        "@llvm-project//mlir:VectorToLLVM",
        "@llvm-project//mlir:VectorToSCF",
        "@llvm-project//mlir:VectorTransforms",
    ],
)

iree_tablegen_doc(
    name = "CodegenLLVMGPUPassesDocGen",
    category = "Passes",
    tbl_outs = [
        (
            [
                "--gen-pass-doc",
            ],
            "CodegenLLVMGPUPasses.md",
        ),
    ],
    tblgen = "@llvm-project//mlir:mlir-tblgen",
    td_file = "Passes.td",
    deps = [
        "@llvm-project//mlir:PassBaseTdFiles",
    ],
)
